<?php
namespace Modules\Activity\Http\Controllers\Client\Jika;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Jika\Activity;
use \Modules\Chip\Entities\Chip;
// use \Modules\User\Entities\User;

use Validator;
use ThrowException;
use Auth;
use DB;

// 参与活动的用户（is_lucker_user）
class ActivityController extends Controller
{
    // 列表
    public function index(Request $req)
    {
        $options = $req->all();
        // $options['per_page'] = 200;
        $options['scopes'] = ['onStock'];
        $options['only'] = [
            'id',
            'uuid',
            'money_price',
            'score_price',
            'title',
            'thumb',
            'activity_type_text',
            'activity_type'
        ];

        $list = Activity::getList($options);

        return $list->generateListResponse();
    }

    // 详情
    public function show(Request $req, $uuid)
    {
        $item = Activity::getEntity([
            'uuid' => $uuid,
            'with' => ['tasks.skus']
        ]);

        $item->isOnStock() ?: ThrowException::Conflict('活动已下架~');

         // 读取所有碎片
        $item->tasks->each(function ($task) {
            $task->chips = $task->getChipList(Auth::user())->toArray();
        });

        $info = $item->getInfo([
            'id',
            'uuid',
            'title',
            'bg',
            'tasks' => [
                '*' => [
                    'sales',
                    'title',
                    'id',
                    'uuid',
                    'sales',
                    'swap_limit',
                    'chips' => [
                        '*' => [
                            'icon',
                            'title',
                            'user_stock'
                        ]
                    ],
                    'skus' => [
                        '*' => [
                            'title',
                            'id',
                            'uuid',
                            'thumb',
                            'stock'
                        ]
                    ],
                    'link'
                ]
            ],
            'page'
        ]);
    
        // 阅读量+1
        $item->update([
            'visit_total' => $item->visit_total + 1
        ]);

        return [
            'info' => $info
        ];
    }

}
